package com.easytoolsoft.easyreport.web.controllers;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

/**
 * 系统属性工具类
 *
 * @author HenryYan
 */
public class PropertyFileUtil {
	
	 private static Logger logger = LoggerFactory.getLogger(PropertyFileUtil.class);
	public static void flushCONF(String layoutJsonPath) {
		List<File> fileList = new ArrayList<File>();
		try {
			layoutJsonPath = URLDecoder.decode(layoutJsonPath, "utf-8");
		} catch (UnsupportedEncodingException e1) {
			logger.error("路径转换出错："+e1.getMessage());
		}
		traverseFolder(layoutJsonPath, fileList);

		Constant.LOAD =null;
		for (File file : fileList) {
			Config parseURL = ConfigFactory.parseFile(file);
			if (Constant.LOAD == null) {
				Constant.LOAD = ConfigFactory.load(parseURL);
			} else {
				Constant.LOAD = Constant.LOAD.withFallback(parseURL);
			}
		}
	}

	public static void traverseFolder(String path, List<File> fileList) {
		File file = new File(path);
		if (file.exists()) {
			File[] files = file.listFiles();
			if (files.length == 0) {
				return;
			} else {
				for (File file2 : files) {
					if (file2.isDirectory()) {
						traverseFolder(file2.getAbsolutePath(), fileList);
					} else {
						fileList.add(file2);
					}
				}
			}
		} else {
			logger.error("文件不存在!" + path);
		}
	}
	
	
	
	
	
	
}